# In [5]: x = torch.tensor(1.)

# In [6]: x
# Out[6]: tensor(1.)

# In [7]: w1 = torch.tensor(2.,requires_grad=True)

# In [8]: b1 = torch.tensor(1.)

# In [9]: w2 = torch.tensor(2.,requires_grad=True)

# In [10]: b2 = torch.tensor(1.)

# In [11]: y1 = x*w1 + b1

# In [12]: y2 = y1*w2 + b2

# In [13]: dy2_dy1 = torch.autograd.grad(y2,[y1], retain_graph=True)[0]

# In [15]: dy1_dw1 = torch.autograd.grad(y1,[w1],retain_graph=True)[0]

# In [16]: dy2_dw1 = torch.autograd.grad(y2,[w1],retain_graph=True)[0]

# In [17]: dy2_dy1*dy1_dw1
# Out[17]: tensor(2.)

# In [18]: dy2_dw1
# Out[18]: tensor(2.)